/*
 * @Author      : Xiaole
 * @Date        : 2024-05-03 12:38:28
 * @LastEditors : Xiaolele
 * @LastEditTime: 2024-05-05 23:16:00
 * @FilePath    : \class10\Drivers\BSP\src\PWM.C
 * @Description : 
 */

#inlude "stm32f4xx_hal.h"
#include "PWM.h"

TIM_HandleTypeDef g_tim_timx_pwm_chy_handle;   //定时器句柄类型变量


/**
 * @Description:  通用定时器PWM输出初始化函数
 * @param {uint16_t} arr   预分频数
 * @param {uint16_t} psc   重装载值
 * @return {*}   无
 */
void gtim_timx_pwm_chy_init(uint16_t arr,uint16_t psc)
{
    g_tim_timx_pwm_chy_handle.Instance = TIM4;        //定时器选择
    g_tim_timx_pwm_chy_handle.Init.Prescaler = psc;   //预分频数
    g_tim_timx_pwm_chy_handle.Init.Period = arr;      //重装载值
    g_tim_timx_pwm_chy_handle.Init.CounterMode = TIM_COUNTERMODE_UP;   //向上递增模式

    HAL_TIM_PWM_Init(&g_tim_timx_pwm_chy_handle);   //1.配置定时器基础工作参数

    TIM_OC_InitTypeDef timx_oc_pwm_chy;
    
    timx_oc_pwm_chy.OCMode = TIM_OCMODE_PWM1;             /*PWM模式1*/
    timx_oc_pwm_chy.Pulse = arr / 2;                      /*占空比50%*/
    timx_oc_pwm_chy.OCPolarity = TIM_OCPOLARITY_LOW;     /*输出低电平有效*/
    /*3.配置PWM模式/比较值等*/
    HAL_TIM_PWM_ConfigChannel(&g_tim_timx_pwm_chy_handle, &timx_oc_pwm_chy,TIM_CHANNEL_2);
    /*4.使能输出并启动计数器*/
    HAL_TIM_PWM_Start(&g_tim_timx_pwm_chy_handle, TIM_CHANNEL_2);


}

/**
 * @Description: 2.定时器输出PWM MSP初始化函数
 * @param {TIM_HandleTypeDef} *htim   
 * @return {*}
 */
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
{
    if(htim->Instance == TIM3)
    {
    GPIO_InitTypeDef gpioledInittHandler;
    __HAL_RCC_GPIOD_CLK_ENABLE();                            /*led引脚时钟使能打开*/
    __HAL_RCC_TIM4_CLK_ENABLE();                            /*tim3时钟打开*/
//    __HAL_RCC_AFIO_CLK_ENABLE();                            //重映射时钟打开
    gpioledInittHandler.Pin   = GPIO_PIN_13;                /*引脚13*/
    gpioledInittHandler.Mode  = GPIO_MODE_AF_PP;            /*推挽复用*/
    gpioledInittHandler.Pull  = GPIO_PULLDOWN;              /*下拉*/
    gpioledInittHandler.Speed = GPIO_SPEED_FREQ_MEDIUM;     /*中速*/
    gpioledInittHandler.Alternate = GPIO_AF2_TIM4;          /*引脚复用功能*/
    HAL_GPIO_Init(GPIOE , &gpioledInittHandler);
    }
}

